package com.siruier.boss.ui.dialog

import android.app.Dialog
import android.content.Context
import android.text.InputType
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.EditText
import com.siruier.boss.R
import com.siruier.boss.databinding.DialogInputBinding
import com.siruier.boss.databinding.DialogProductSpecBinding
import com.siruier.boss.ui.ext.getString
import com.siruier.boss.ui.utils.Utils

/**
 * 输入页面
 */
class InputDialog(
    context: Context?,
    title: String, hint: String,
    var content: String = "",
    cancelable: Boolean = true,
    settingText: ((editText: EditText) -> Unit)? = null,
    private var dismissLinsnter: (() -> Unit?)? = null,
    private var linsnter: ((String) -> Unit)? = null
) : Dialog(context!!, R.style.dimLoadingdialog) {
    companion object {

        fun showPayPassword(context: Context?, linsnter: ((String) -> Unit)? = null) {
            InputDialog(context, R.string.dialog_pay_password.getString(), R.string.dialog_pay_password_hint.getString(), settingText = {
                it.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
            }, linsnter = linsnter).show()
        }

        fun showMallPayPassword(context: Context?, dismissLinsnter: (() -> Unit?)? = null, linsnter: ((String) -> Unit)? = null) =
            InputDialog(
                context,
                R.string.dialog_pay_password.getString(),
                R.string.dialog_pay_password_hint.getString(),
                cancelable = false,
                settingText = {
                    it.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
                },
                dismissLinsnter = dismissLinsnter,
                linsnter = linsnter
            ).apply { show() }

        fun showActkey(context: Context?, linsnter: ((String) -> Unit)? = null) {
            InputDialog(context, R.string.dialog_actkey.getString(), R.string.dialog_actkey_hint.getString(), settingText = {
                it.inputType = InputType.TYPE_CLASS_NUMBER
            }, linsnter = linsnter).show()
        }
    }

    private val vb: DialogInputBinding = DialogInputBinding.inflate(LayoutInflater.from(context))

    init {
        setContentView(vb.root)
        settingText?.invoke(vb.etContent)
        vb.tvTitle.text = title
        vb.etContent.hint = hint
        vb.buttonConfirm.setOnClickListener {
            linsnter?.let {
                if (vb.etContent.text.toString().trim().isNotEmpty()) {
                    it(vb.etContent.text.toString())
                    Utils.hideSoftInput(vb.etContent)
                    dismiss()
                }
            }
        }
        vb.ivClose.setOnClickListener { dismissLinsnter?.invoke();dismiss() }
        setCancelable(cancelable)
        window?.let {
            val attributes = it.attributes
            attributes.width = (getContext().resources.displayMetrics.widthPixels * 0.92).toInt()
            attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT
            it.attributes = attributes
            it.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
        }
    }

    fun showWithContent(newContent: String = content) {
        content = newContent
        show()
    }

    override fun show() {
        super.show()
        vb.etContent.setText(content)
        vb.etContent.isFocusable = true
        vb.etContent.isFocusableInTouchMode = true
        vb.etContent.requestFocus()
        content = ""
    }
}